<?php
require_once 'common/config.php';
require_once 'common/functions.php';
// 检查登录状态
check_login();
$db = db_connect();
// 获取搜索和筛选参数
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$type = isset($_GET['type']) ? trim($_GET['type']) : '';
$status = isset($_GET['status']) ? intval($_GET['status']) : -1;
$date = isset($_GET['date']) ? trim($_GET['date']) : '';
// 每页显示的记录数
$per_page = 10;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($page - 1) * $per_page;
// 构建查询条件
$where = "1=1";
$params = [];
if ($search) {
    $where .= " AND (f.title LIKE :search OR f.content LIKE :search OR u.name LIKE :search)";
    $params[':search'] = "%{$search}%";
}
if ($type) {
    $where .= " AND f.type = :type";
    $params[':type'] = $type;
}
if ($status != -1) {
    $where .= " AND f.status = :status";
    $params[':status'] = $status;
}
if ($date) {
    $where .= " AND DATE(f.created_at) = :date";
    $params[':date'] = $date;
}
// 获取总记录数
$count_sql = "SELECT COUNT(*) FROM feedbacks f 
               LEFT JOIN users u ON f.user_id = u.id 
               WHERE {$where}";
$count_stmt = $db->prepare($count_sql);
foreach ($params as $key => $value) {
    $count_stmt->bindValue($key, $value);
}
$count_stmt->execute();
$total_records = $count_stmt->fetchColumn();
$total_pages = ceil($total_records / $per_page);
// 获取反馈列表（联表查询用户信息）
$sql = "SELECT f.*, u.name as user_name, 
        a.name as admin_name 
        FROM feedbacks f 
        LEFT JOIN users u ON f.user_id = u.id 
        LEFT JOIN admin_users a ON f.admin_id = a.id 
        WHERE {$where} 
        ORDER BY f.created_at DESC
        LIMIT {$offset}, {$per_page}";

$stmt = $db->prepare($sql);
foreach ($params as $key => $value) {
    $stmt->bindValue($key, $value);
}
$stmt->execute();
$feedbacks = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 反馈类型映射
$feedback_types = [
    'feature' => ['name' => '功能建议', 'badge' => 'badge-primary'],
    'bug' => ['name' => '问题反馈', 'badge' => 'badge-danger'],
    'other' => ['name' => '其他', 'badge' => 'badge-secondary']
];
// 状态映射
$status_types = [
    0 => ['name' => '待处理', 'badge' => 'badge-warning'],
    1 => ['name' => '已回复', 'badge' => 'badge-success'],
    2 => ['name' => '已关闭', 'badge' => 'badge-secondary']
];
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>意见反馈管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
    .filter-bar {
        background: #fff;
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        margin-bottom: 20px;
    }
    .search-bar {
        display: flex;
        gap: 20px;
        align-items: center;
        flex-wrap: wrap;
        position: relative;
    }
    .search-bar input[type="text"] {
        flex: 1;
        min-width: 200px;
        padding: 10px 15px;
        padding-left: 40px;
        border: 1px solid #ddd;
        border-radius: 4px;
        font-size: 14px;
        transition: border-color 0.3s;
        background: #f8f9fa;
    }
    .search-bar input[type="text"]:focus {
        outline: none;
        border-color: #3498db;
        background: #fff;
    }
    .search-bar::before {
        content: '\f002';
        font-family: 'Font Awesome 5 Free';
        font-weight: 900;
        position: absolute;
        left: 15px;
        top: 50%;
        transform: translateY(-50%);
        color: #6c757d;
        font-size: 14px;
    }
    .search-bar select {
        padding: 10px 35px 10px 15px;
        border: 1px solid #ddd;
        border-radius: 4px;
        background: #f8f9fa url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8' viewBox='0 0 8 8'%3E%3Cpath fill='%23666' d='M0 2l4 4 4-4z'/%3E%3C/svg%3E") no-repeat right 15px center;
        font-size: 14px;
        min-width: 120px;
        cursor: pointer;
        appearance: none;
        -webkit-appearance: none;
        transition: all 0.3s;
    }
    .search-bar select:focus {
        outline: none;
        border-color: #3498db;
        background-color: #fff;
    }
    .search-bar input[type="date"] {
        padding: 9px 15px;
        border: 1px solid #ddd;
        border-radius: 4px;
        background: #f8f9fa;
        font-size: 14px;
        cursor: pointer;
        transition: all 0.3s;
        min-width: 150px;
    }
    .search-bar input[type="date"]:focus {
        outline: none;
        border-color: #3498db;
        background: #fff;
    }
    .search-bar button {
        padding: 10px 25px;
        background: #3498db;
        color: #fff;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        transition: background 0.3s;
        font-weight: 500;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .search-bar button:hover {
        background: #2980b9;
        transform: translateY(-1px);
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .search-bar button i {
        font-size: 14px;
    }
    @media (max-width: 768px) {
        .search-bar {
            flex-direction: column;
            align-items: stretch;
        }
        .search-bar input[type="text"],
        .search-bar select,
        .search-bar input[type="date"] {
            width: 100%;
        }
        .search-bar button {
            width: 100%;
            justify-content: center;
        }
    }
    .feedback-content {
        max-width: 300px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .feedback-images {
        display: flex;
        gap: 5px;
    }
    .feedback-image {
        width: 50px;
        height: 50px;
        object-fit: cover;
        border-radius: 4px;
        cursor: pointer;
        transition: transform 0.2s;
    }
    .feedback-image:hover {
        transform: scale(1.05);
    }
    .user-info {
        font-size: 0.9em;
        color: #666;
    }
    .data-table table th:nth-child(6),
    .data-table table th:nth-child(7) {
        min-width: 110px;
    }
    .badge {
        display: inline-block;
        padding: 5px 10px;
        white-space: nowrap;
    }
    </style>
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>意见反馈管理</h2>
                </div>
            </header>

            <div class="content">
                <!-- 搜索和筛选 -->
                <div class="filter-bar">
                    <form action="" method="GET" id="searchForm" class="search-bar">
                        <input type="text" name="search" placeholder="搜索反馈标题、内容或用户名..." 
                               value="<?php echo htmlspecialchars($search); ?>">
                        <select name="type" onchange="document.getElementById('searchForm').submit()">
                            <option value="">全部类型</option>
                            <?php foreach ($feedback_types as $key => $value): ?>
                            <option value="<?php echo $key; ?>" <?php echo $type === $key ? 'selected' : ''; ?>>
                                <?php echo $value['name']; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                        <select name="status" onchange="document.getElementById('searchForm').submit()">
                            <option value="-1">全部状态</option>
                            <?php foreach ($status_types as $key => $value): ?>
                            <option value="<?php echo $key; ?>" <?php echo $status === $key ? 'selected' : ''; ?>>
                                <?php echo $value['name']; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                        <input type="date" name="date" value="<?php echo $date; ?>" 
                               onchange="document.getElementById('searchForm').submit()"
                               placeholder="选择日期">
                        <button type="submit"><i class="fas fa-search"></i> 搜索</button>
                    </form>
                </div>

                <!-- 数据表格 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th width="5%">ID</th>
                                <th width="10%">用户信息</th>
                                <th width="10%">标题</th>
                                <th width="20%">内容</th>
                                <th width="20%">图片</th>
                                <th width="8%">类型</th>
                                <th width="7%">状态</th>
                                <th width="10%">提交时间</th>
                                <th width="10%">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($feedbacks as $feedback): ?>
                            <tr>
                                <td><?php echo $feedback['id']; ?></td>
                                <td>
                                    <div class="user-info">
                                        <div><?php echo htmlspecialchars($feedback['user_name']); ?></div>
                                    </div>
                                </td>
                                <td><?php echo htmlspecialchars($feedback['title']); ?></td>
                                <td class="feedback-content"><?php echo htmlspecialchars($feedback['content']); ?></td>
                                <td>
                                    <?php if ($feedback['images']): ?>
                                    <div class="feedback-images">
                                        <?php foreach (explode(',', $feedback['images']) as $image): ?>
                                        <img src="<?php echo '../api/user/' . htmlspecialchars($image); ?>" 
                                             class="feedback-image" 
                                             onclick="showImage(this.src)"
                                             alt="反馈图片">
                                        <?php endforeach; ?>
                                    </div>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <span class="badge <?php echo $feedback_types[$feedback['type']]['badge']; ?>">
                                        <?php echo $feedback_types[$feedback['type']]['name']; ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge <?php echo $status_types[$feedback['status']]['badge']; ?>">
                                        <?php echo $status_types[$feedback['status']]['name']; ?>
                                    </span>
                                </td>
                                <td><?php echo date('Y-m-d H:i', strtotime($feedback['created_at'])); ?></td>
                                <td>
                                    <a href="feedback_reply.php?id=<?php echo $feedback['id']; ?>" 
                                       class="btn-icon" title="查看/回复">
                                        <i class="fas fa-reply"></i>
                                    </a>
                                    <?php if ($feedback['status'] != 2): ?>
                                    <a href="javascript:void(0)" 
                                       onclick="closeFeedback(<?php echo $feedback['id']; ?>)"
                                       class="btn-icon text-warning" title="关闭反馈">
                                        <i class="fas fa-times-circle"></i>
                                    </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- 添加分页 -->
                <?php 
                echo generate_pagination($page, $total_pages, [
                    'search' => $search,
                    'type' => $type,
                    'status' => $status,
                    'date' => $date
                ]); 
                ?>
            </div>
        </div>
    </div>

    <!-- 图片预览模态框 -->
    <div id="imageModal" class="modal" onclick="this.style.display='none'">
        <img id="modalImage" class="modal-content">
    </div>

    <script>
    // 图片预览
    function showImage(src) {
        const modal = document.getElementById('imageModal');
        const modalImg = document.getElementById('modalImage');
        modal.style.display = "block";
        modalImg.src = src;
    }

    // 关闭反馈
    function closeFeedback(id) {
        if (confirm('确定要关闭这条反馈吗？')) {
            window.location.href = `feedback_close.php?id=${id}`;
        }
    }
    </script>

    <style>
    .modal {
        display: none;
        position: fixed;
        z-index: 1000;
        padding-top: 50px;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0,0,0,0.9);
        cursor: pointer;
    }
    .modal-content {
        margin: auto;
        display: block;
        max-width: 80%;
        max-height: 80%;
    }
    </style>
</body>
</html> 